import 'dart:math';

import 'package:flutter/material.dart';

class CustomPaintRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("五子棋")),
      body: Stack(
        children: <Widget>[
          Positioned(
            top: 100.0,
            left: 100.0,
            child: CustomPaint(
              size: Size(300, 300),
              painter: MyPainter(),
            ),
          )
        ],
      ),
    );
  }
}

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    int num = 10;
    double eWidth = size.width / num; // 棋盘有几个格
    double eHeight = size.height / num;
    print('eWidth: $eWidth');
    print('eHeight: $eHeight');
    final paint = Paint()
      ..isAntiAlias = true
      ..style = PaintingStyle.fill // 填充
      ..color = Color(0x77cdb175); // 背景色 纸黄色

    // // 画棋盘的背景
    // canvas.drawRect(Offset.zero & size, paint);

    // paint
    //   ..style = PaintingStyle.stroke // 线
    //   ..color = Colors.black87 // 线的颜色
    //   ..strokeWidth = 1.0; // 线的宽度

    // for (var i = 0; i <= num; i++) {
    //   double dy = eHeight * i;
    //   // 竖线
    //   canvas.drawLine(Offset(0, dy), Offset(size.width, dy), paint);
    // }

    // for (var i = 0; i <= num; i++) {
    //   double dx = eWidth * i;
    //   // 横线
    //   canvas.drawLine(Offset(dx, 0), Offset(dx, size.height), paint);
    // }

    //画一个黑子
    paint
      ..style = PaintingStyle.fill
      ..color = Colors.black;
    // 画圆
    print(
        'eWidth: ${size.width / 2 - eWidth / 2}, eHeight: ${size.height / 2 - eHeight / 2}');

    canvas.drawCircle(
      Offset(150, 150),
      min(eWidth / 2, eHeight / 2) - 2,
      paint,
    );

    //画一个白子
    paint.color = Colors.white;
    canvas.drawCircle(
      Offset(180, 150),
      min(eWidth / 2, eHeight / 2) - 2,
      paint,
    );
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
